﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class apps_wlog_wlogEdit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            if (string.IsNullOrEmpty(Request.QueryString["wlid"] ))
            {
                Response.Redirect("wlogList.aspx");
            }
            System.Web.Security.MembershipUser m = System.Web.Security.Membership.GetUser();
            this.ddlResources.Items.Clear();
            ListItemCollection llii = cUIUtili.getResourceComboItem(m.UserName);
            foreach (ListItem li in llii)
            {
                this.ddlResources.Items.Add(li);
            }
            if (Request.QueryString["wlid"] != "N")
            {
                fillDataForm(new Guid(Request.QueryString["wlid"]));
                fillJobsDdl(-1);
            }
            else
            {
                fillJobsDdl(0);
            }
        }
    }
    private void fillJobsDdl(int status)
    {
        System.Data.DataTable dt = new System.Data.DataTable("TABJOB");
        string sSql = "SELECT JobId, '[' + Trim([JobCode]) + '] ' + Trim([Description]) AS Job, Status FROM Jobs";
        if (status > -1)
        {
            sSql += " WHERE Status=" + status;
        }
        try
        {
            System.Data.OleDb.OleDbConnection cn = new System.Data.OleDb.OleDbConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter(sSql, cn);
            cn.Open();
            da.Fill(dt);
            cn.Close();
        }
        catch (System.Data.OleDb.OleDbException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw e;
        }
        //this.ASPxComboBoxJobs.DataSource = dt;
        //this.ASPxComboBoxJobs.DataMember = "TABJOB";
        //this.ASPxComboBoxJobs.ValueField = "JobId";
        //this.ASPxComboBoxJobs.TextField = "Job";
        //this.ASPxComboBoxJobs.DataBind();
    }
    private void fillJobTaskDdl(Guid jobId, int status)
    {
        System.Data.DataTable dt = new System.Data.DataTable("TABJOBTASKS");
        string sSql = "SELECT TaskId, '[' + Trim([TaskId]) + '] ' + Trim([Description]) AS Task, Status FROM JobTasks WHERE JobId=?";
        if (status > -1)
        {
            sSql += " AND Status=" + status;
        }
        sSql += " ORDER BY TaskId;";
        try
        {
            System.Data.OleDb.OleDbConnection cn = new System.Data.OleDb.OleDbConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            System.Data.OleDb.OleDbCommand cm = new System.Data.OleDb.OleDbCommand(sSql, cn);
            cm.Parameters.Add("JobId", System.Data.OleDb.OleDbType.Guid);
            cm.Parameters["JobId"].Value = jobId;
            System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter(cm);
            cn.Open();
            da.Fill(dt);
            cn.Close();
        }
        catch (System.Data.OleDb.OleDbException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw e;
        }
        this.ddlJobTask.DataSource = dt;
        this.ddlJobTask.DataMember = "TABJOBTASKS";
        this.ddlJobTask.DataValueField = "TaskId";
        this.ddlJobTask.DataTextField = "Task";
        this.ddlJobTask.DataBind();
    }
    private void fillDataForm(Guid workLogId)
    {
        System.Data.DataTable dt = new System.Data.DataTable("TABWL");
        // Jobs.JobCode, Jobs.Description AS JobDescription, JobTasks.Description AS TaskDescription, FROM ((WorkLog LEFT OUTER JOIN Jobs ON WorkLog.JobId = Jobs.JobId) LEFT OUTER JOIN JobTasks ON WorkLog.JobId = JobTasks.JobId AND WorkLog.JobTaskId = JobTasks.TaskId)
        string sSql = "SELECT WorkLog.WorkLogId, WorkLog.DayDate, WorkLog.JobId, WorkLog.JobTaskId, WorkLog.NumberOfUnit, WorkLog.UnitMeasures, WorkLog.Status, WorkLog.ResourceId, WorkLog.WorkLogNote,  WorkLog.StatusChaged, WorkLog.StatusNote FROM [WorkLog] WHERE [WorkLog].[WorkLogId] = ?;";
        try
        {
            System.Data.OleDb.OleDbConnection cn = new System.Data.OleDb.OleDbConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            System.Data.OleDb.OleDbCommand cm = new System.Data.OleDb.OleDbCommand(sSql, cn);
            cm.Parameters.Add("WorkLogId", System.Data.OleDb.OleDbType.Guid);
            cm.Parameters["WorkLogId"].Value = workLogId;
            cn.Open();
            System.Data.OleDb.OleDbDataReader dr = cm.ExecuteReader();
            while (dr.Read())
            {
                this.HiddenFieldWorkLogId.Value = dr.GetGuid(0).ToString("B");
                this.ddlResources.SelectedValue = dr.GetGuid(7).ToString("B");
                // Jobs e TskJobs
                //this.ASPxComboBoxJobs.Value = dr.GetGuid(2);
                fillJobTaskDdl(dr.GetGuid(2), -1);
                this.ddlJobTask.SelectedValue = dr.GetInt32(3).ToString();
                this.textboxNumberOfUnit.Text = dr.GetDecimal(4).ToString();
                this.ddlUnitOfMeasure.Text = dr.GetString(5);
                this.textboxDayDate.Text = dr.GetDateTime(1).ToShortDateString();
                this.TextBoxWorkLogNote.Text = (dr.IsDBNull(8) ? string.Empty : dr.GetString(8));
                this.LabelStatus.Text = dr.GetInt32(6).ToString();
                this.LabelChanged.Text = (dr.IsDBNull(9) ? string.Empty : dr.GetDateTime(9).ToShortDateString());
                this.LabelNote.Text = (dr.IsDBNull(10) ? string.Empty : dr.GetString(10)); 
            }
            dr.Close();
            cn.Close();
        }
        catch (System.Data.OleDb.OleDbException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw e;
        }

        
    }

    protected void ASPxComboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //Guid g = new Guid(this.ASPxComboBoxJobs.Value.ToString());
        //fillJobTaskDdl(g, -1);
    }
}
